package com.guonl.controller;

import com.alibaba.fastjson.JSON;
import com.guonl.open.api.client.OpenApiWaybillClient;
import com.guonl.open.api.config.OpenApiProperties;
import com.guonl.open.api.request.waybill.*;
import com.guonl.open.api.response.OpenApiResult;
import com.guonl.open.api.response.waybill.ApiTransportNoteInfoResponse;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/client/openapi")
public class OpenApiClientController {

    @Autowired
    private OpenApiProperties openApiProperties;

    @Autowired
    private OpenApiWaybillClient openApiWaybillClient;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @ApiOperation(value = "创建运单", notes = "创建运单")
    @PostMapping("/create")
    public OpenApiResult<ApiTransportNoteInfoResponse> clientCreateTransportNote() {

        log.info("调用开始-------------->>>>>");
        long ss = System.currentTimeMillis();

        List<OpenApiProperties.ApiKeyInfo> apiKeyInfoList = openApiProperties.getApiKeyList();
        OpenApiProperties.ApiKeyInfo apiKeyInfo = apiKeyInfoList.get(0);
        String apiKey = apiKeyInfo.getApiKey();

        String outSideWaybillNO = "api-00007";
        ApiCreateTransportNoteRequest transportNoteRequest = buildTransportNoteRequest(outSideWaybillNO);
        long start = System.currentTimeMillis();
        OpenApiResult<ApiTransportNoteInfoResponse> transportNote = null;
        try {
            transportNote = openApiWaybillClient.createTransportNote(transportNoteRequest, apiKey);
        } catch (Exception e) {
            log.error("调用失败，失败原因:{}", e.getMessage());
        }
        log.info("调用结果:{}", JSON.toJSONString(transportNote));
        long end = System.currentTimeMillis();
        log.info("总耗时:{}毫秒", (end - ss));


        ApiTransportNoteInfoResponse transportNoteInfo = new ApiTransportNoteInfoResponse();
        return OpenApiResult.ok(transportNoteInfo);
    }

    private ApiCreateTransportNoteRequest buildTransportNoteRequest(String outSideWaybillNO) {
        ApiCreateTransportNoteRequest target = new ApiCreateTransportNoteRequest();
        target.setOutSideWaybillNO(outSideWaybillNO);
        target.setExtendId("api-extend-id");
        target.setBillTime("2024-03-27 08:00:00");
        target.setChannel("测试open api");

        target.setWaybillInfo(buildApiWaybillInfoRequest());
        target.setDriverInfo(buildApiDriverInfoRequest());
        target.setAgreementInfo(buildApiAgreementInfoRequest());
        target.setLoadingInfo(buildApiLoadingInfoRequest());
        target.setUnloadingInfo(buildApiUnloadingInfoRequest());
        target.setReceiptInfo(buildApiUploadReceiptInfoRequest());
        return target;
    }

    private ApiWaybillInfoRequest buildApiWaybillInfoRequest() {
        ApiWaybillInfoRequest target = new ApiWaybillInfoRequest();
//    target.setHaulDistance(new BigDecimal("100"));
        target.setFreightAmount(new BigDecimal("100"));
        target.setCreateTime(sdf.format(new Date()));
        return target;
    }

    private ApiDriverInfoRequest buildApiDriverInfoRequest() {
        ApiDriverInfoRequest target = new ApiDriverInfoRequest();
//    target.setDriverName(source.getDriverName());
        target.setDriverPhone("18607770094");
//    target.setDriverIdCardNumber("450703199809281874");非必填参数
        target.setVehicleLicenseNumber("苏AA2211");
        return target;
    }

    private ApiAgreementInfoRequest buildApiAgreementInfoRequest() {
        ApiAgreementInfoRequest target = new ApiAgreementInfoRequest();
        target.setInitiationTime(sdf.format(new Date()));
        target.setConfirmAgreementTime(sdf.format(new Date()));
        target.setDriverAgreementUrl("/test/123.pdf");
        target.setShipperAgreementUrl("/test/456.pdf");
        return target;
    }

    private ApiLoadingInfoRequest buildApiLoadingInfoRequest() {
        ApiLoadingInfoRequest target = new ApiLoadingInfoRequest();
//    target.setLoadingAreaId("330102");
        target.setLoadingAddress("浙江省杭州市上城区解放东路2号");
        target.setLoadingTime("2024-03-29 08:00:00");
        target.setLoadingImage("/test/loading/111.jpg");
        target.setLoadingNumber(new BigDecimal(1000));
//    target.setLoadingLongitude(source.getLoadingLongitude());
//    target.setLoadingLatitude(source.getLoadingLatitude());
        return target;
    }

    private ApiUnloadingInfoRequest buildApiUnloadingInfoRequest() {
        ApiUnloadingInfoRequest target = new ApiUnloadingInfoRequest();
//    target.setUnloadAreaId("310118");
        target.setUnloadAddress("上海市青浦区赵巷镇佳杰路88号");
        target.setUnloadingTime("2024-03-29 15:00:00");
        target.setUnloadImage("/test/loading/222.jpg");
        target.setUnloadNumber(new BigDecimal(1000));
//    target.setUnloadLongitude("111");
//    target.setUnloadLatitude("111");
        return target;
    }

    private ApiUploadReceiptInfoRequest buildApiUploadReceiptInfoRequest() {
        ApiUploadReceiptInfoRequest target = new ApiUploadReceiptInfoRequest();
        target.setReceiptImage("/test/receipt/333.jpg");
        target.setReceiptTime("2024-03-29 15:01:00");
        return target;
    }


}
